package com.pm.service.impl;

import com.pm.entity.DailyReport;
import com.pm.mapper.DailyReportMapper;
import com.pm.service.DailyReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * 日报服务实现类
 */
@Service
public class DailyReportServiceImpl implements DailyReportService {
    
    @Autowired
    private DailyReportMapper dailyReportMapper;
    
    @Override
    public DailyReport createDailyReport(DailyReport dailyReport) {
        // 检查当天是否已经提交过日报
        DailyReport existing = findByUserIdAndDate(dailyReport.getUserId(), dailyReport.getReportDate());
        if (existing != null) {
            throw new RuntimeException("今日已提交日报，请勿重复提交");
        }
        
        dailyReport.setCreatedAt(LocalDateTime.now().toString());
        dailyReport.setUpdatedAt(LocalDateTime.now().toString());
        dailyReportMapper.insert(dailyReport);
        return dailyReport;
    }
    
    @Override
    public DailyReport updateDailyReport(DailyReport dailyReport) {
        dailyReport.setUpdatedAt(LocalDateTime.now().toString());
        dailyReportMapper.updateById(dailyReport);
        return dailyReport;
    }
    
    @Override
    public boolean deleteDailyReport(String id) {
        return dailyReportMapper.deleteById(id) > 0;
    }
    
    @Override
    public DailyReport findById(String id) {
        return dailyReportMapper.selectById(id);
    }
    
    @Override
    public List<DailyReport> getAllDailyReports() {
        return dailyReportMapper.findAll();
    }
    
    @Override
    public List<DailyReport> findByUserId(String userId) {
        return dailyReportMapper.findByUserId(userId);
    }
    
    @Override
    public List<DailyReport> findByProjectId(String projectId) {
        return dailyReportMapper.findByProjectId(projectId);
    }
    
    @Override
    public DailyReport findByUserIdAndDate(String userId, String reportDate) {
        return dailyReportMapper.findByUserIdAndDate(userId, reportDate);
    }
    
    @Override
    public List<DailyReport> findByDateRange(String startDate, String endDate) {
        return dailyReportMapper.findByDateRange(startDate, endDate);
    }
    
    @Override
    public List<DailyReport> findByUserIdAndDateRange(String userId, String startDate, String endDate) {
        return dailyReportMapper.findByUserIdAndDateRange(userId, startDate, endDate);
    }
    
    @Override
    public boolean hasSubmittedToday(String userId) {
        DailyReport report = findByUserIdAndDate(userId, LocalDate.now().toString());
        return report != null;
    }
    
    @Override
    public Double getWeeklyWorkHours(String userId) {
        LocalDate today = LocalDate.now();
        LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY));
        LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY));
        
        List<DailyReport> reports = findByUserIdAndDateRange(userId, startOfWeek.toString(), endOfWeek.toString());
        return reports.stream()
                .mapToDouble(report -> report.getWorkHours() != null ? report.getWorkHours() : 0.0)
                .sum();
    }
    
    @Override
    public Double getMonthlyWorkHours(String userId, int month) {
        LocalDate today = LocalDate.now();
        LocalDate startOfMonth = LocalDate.of(today.getYear(), month, 1);
        LocalDate endOfMonth = startOfMonth.with(TemporalAdjusters.lastDayOfMonth());
        
        List<DailyReport> reports = findByUserIdAndDateRange(userId, startOfMonth.toString(), endOfMonth.toString());
        return reports.stream()
                .mapToDouble(report -> report.getWorkHours() != null ? report.getWorkHours() : 0.0)
                .sum();
    }
    
    @Override
    public boolean hasSubmittedTodayReport(String userId) {
        return hasSubmittedToday(userId);
    }
}